package flc.ast.activity;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityAppointCompressBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.Str2NumUtil;

public class AppointCompressActivity extends BaseAc<ActivityAppointCompressBinding> {

    public static String imgPath;
    private double size, compressSize;

    @Override
    protected int onCreate() {
        return R.layout.activity_appoint_compress;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAppointCompressBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvStartCompress.setOnClickListener(this);

        Glide.with(mContext).load(imgPath).into(mDataBinding.ivCompressImg);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvStartCompress:
                String strMin = mDataBinding.etMin.getText().toString();
                String strMax = mDataBinding.etMax.getText().toString();
                if (TextUtils.isEmpty(strMin) || TextUtils.isEmpty(strMax)) {
                    ToastUtils.showShort(getString(R.string.size_not_null));
                    return;
                }
                double min = Str2NumUtil.parse(strMin);
                double max = Str2NumUtil.parse(strMax);
                if (min == 0 || max == 0) {
                    ToastUtils.showShort(getString(R.string.size_not_zero));
                    return;
                }
                if (min > size || max > size) {
                    ToastUtils.showShort(getString(R.string.not_greater_than_size));
                    return;
                }
                compressSize = (min + max) / 2;
                save();
                break;
        }
    }

    @Override
    protected void initData() {
        long length = FileUtils.getLength(imgPath);
        String formattedValue = String.format("%.2f", ((double) length / 1024));
        Bitmap bitmap = ImageUtils.getBitmap(imgPath);
        mDataBinding.tvPixel.setText(bitmap.getWidth() + "x" + bitmap.getHeight() + getString(R.string.unit_px));
        mDataBinding.tvSize.setText(formattedValue);
        size = Str2NumUtil.parse(formattedValue);
    }

    private void save() {
        Bitmap bitmap = ImageUtils.compressBySampleSize(ImageUtils.getBitmap(imgPath), (int) compressSize);
        //保存到相册
        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
        //保存到最近文件
        String filePath = FileUtil.generateFilePath(Content.myCompress, getString(R.string.unit_png));
        ImageUtils.save(bitmap, filePath, Bitmap.CompressFormat.PNG);
        CompressActivity.isSingle = true;
        CompressActivity.imgPath = imgPath;
        startActivity(CompressActivity.class);
    }

}